<template>
  <div style="width: 400px;text-align:center;margin: 5px auto">
    <h1 STYLE="margin-bottom: 10px;">模板函数编辑</h1>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

      <el-form-item label="函数名称" prop="functionName">
        <el-input type="text" v-model="ruleForm.functionName" autocomplete="off" ></el-input>
      </el-form-item>

      <el-form-item label="操作类名" prop="operatorClass">
        <el-input type="text" v-model="ruleForm.operatorClass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="函数描述" prop="functionDesc">
        <div class="captcha_code">
          <el-input v-model="ruleForm.functionDesc"  @keyup.native="autoSubmit($event)"></el-input>
        </div>
      </el-form-item>
      <el-form-item >
        <div style="text-align:center">
          <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button @click="returnList()">返回</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import request from "../utils/request";

export default {
  name: "Edit",
  data(){
    const validateFunctionName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入函数名"));
      } else {
        callback();
      }
    };
    const validateOperatorClass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入操作类"));
      } else {
        callback();
      }
    };

    return{
      ruleForm: {
        id:'',
        functionName: '',
        operatorClass: '',
        functionDesc: ''
      },

      rules: {
        functionName: [{ required: true,validator: validateFunctionName, trigger: "blur" }],
        operatorClass: [{ required: true,validator: validateOperatorClass, trigger: "blur" }]
      }
    }

  },
  methods: {
    autoSubmit: function (ev) {
      if (ev.code === "Enter") {
        this.submitForm('ruleForm');
      }
    },
    submitForm(formName) {
      let _this = this;
      const param = {
        id:_this.ruleForm.id,
        functionName: _this.ruleForm.functionName,
        operatorClass: _this.ruleForm.operatorClass,
        functionDesc: _this.ruleForm.functionDesc
      };
      this.$refs[formName].validate(valid => {
        if (valid) {
          request({
            method: "POST",
            url: "/api/Config/update",
            data: param
          })
            .then(res => {
              if (res.data.status == "1") {
                this.$message(res.data.message);
                _this.$router.push({path: "/templatefunction/list"});
              } else {
                this.$alert(res.data.message, {
                  confirmButtonText: '确定',
                  callback: action => {
                  }
                });
                return false;
              }
            })
            .catch(err => {
              debugger;
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loadData(id){
      const  that = this;
      request({
        method: "POST",
        url: "/api/Config/getById?id="+id,
      })
        .then(res => {
          if (res.data.status == "1") {
            that.ruleForm.id = res.data.bean.ID;
            that.ruleForm.functionDesc = res.data.bean.FUNCTION_DESC;
            that.ruleForm.functionName = res.data.bean.FUNCTION_NAME;
            that.ruleForm.operatorClass = res.data.bean.OPERATOR_CLASS;
            this.$message(res.data.message);
          } else {
            this.$alert(res.data.message, {
              confirmButtonText: '确定',
              callback: action => {
              }
            });
            return false;
          }
        })
        .catch(err => {
          debugger;
          console.log(err);
        });
    },
    returnList(){
      this.$router.push({path: "/templatefunction/list"});
    }
  },
  mounted() {
    console.log("ID:"+this.$route.params.ID);
    this.ruleForm.id = this.$route.params.id;
    this.loadData(this.ruleForm.id);
  }
}
</script>

<style scoped>

</style>
